package admin

import (
	"encoding/json"
	"io/ioutil"
	"log"
	"src/global"
	"src/model"
	"src/model/ProtoModel"
	service2 "src/service"
)

type Bind struct {
}

var Indexs *Bind

func NewBind() *Bind {
	return &Bind{}
}

// 绑定房间
func (*Bind) BindRoom(b *service2.Req) {
	var err error
	// 接收post参数
	body, err := ioutil.ReadAll(b.R.Body)
	if err != nil {
		log.Println(err)
		b.Fail("读取body失败")
		return
	}
	//var reg model.LoginModelReg
	userBind := model.RoomBindModel{}
	err = json.Unmarshal(body, &userBind)
	if err != nil {
		b.Fail("json格式失败")
		return
	}

	reply := &ProtoModel.ReplyBodyProto{
		Key:  "room_bind",
		Code: "200",
		Data: make(map[string]string),
	}
	if userBind.RoomNo == "" {
		b.Fail("RoomNo 不能为空")
		return
	}
	if userBind.Uid == "" {
		b.Fail("Uid 不能为空")
		return
	}
	cons := global.Ws_.UserList[userBind.Uid]
	if cons != nil {
		var messageProto ProtoModel.MessageProto
		for _, connId := range cons {

			//connId := cons[0]
			conn := global.Ws_.ConnList[connId]
			oldRoomNo := conn.RoomNo
			//删除直接绑定过的房间号
			if oldRoomNo != "" {
				(service2.AuthI).DelOldRoomBind(userBind.Uid, oldRoomNo)
			}
			global.Ws_.ConnList[connId].RoomNo = userBind.RoomNo
			room := global.Ws_.RoomList[userBind.RoomNo]
			//房间不存在，创建房间
			if room == nil {
				//a:=make(map[string]model.RoomBindModel)
				room = make(map[string]string)
				room[conn.Id] = conn.Uid
				//绑定房间用户数据
				global.Ws_.RoomList[userBind.RoomNo] = room
			} else {
				//绑定房间 用户数据
				room[conn.Id] = conn.Uid
				//绑定房间用户数据
				global.Ws_.RoomList[userBind.RoomNo] = room
			}
			(&service2.ReplyBody{}).SendMsgToConn(reply, conn.Id)
			messageProto.Action = "user_in_room"
			messageProto.Title = "房间消息"
			messageProto.Content = make(map[string]string)
			messageProto.Content["roomNo"] = conn.RoomNo
			messageProto.Content["uid"] = conn.Uid
			messageProto.Content["nickname"] = conn.Nickname
			messageProto.Content["avatar"] = conn.Avatar
			messageProto.Content["msg"] = conn.Nickname + "进入房间"
			(&service2.Message{}).SendMsgToRoom(&messageProto, conn.RoomNo)
		}
	}

	if err != nil {
		b.Fail("链接不存在")
		return
	}
	b.Ok("成功")
}

// 退出房间
func (*Bind) OutRoom(b *service2.Req) {

	var err error
	// 接收post参数
	body, err := ioutil.ReadAll(b.R.Body)
	if err != nil {
		log.Println(err)
	}
	//var reg model.LoginModelReg
	userBind := model.RoomBindModel{}
	err = json.Unmarshal(body, &userBind)
	if err != nil {
		return
	}

	reply := &ProtoModel.ReplyBodyProto{
		Key:  "room_out",
		Code: "200",
		Data: make(map[string]string),
	}
	//global.Ws_.
	cons := global.Ws_.UserList[userBind.Uid]
	if cons != nil {
		for _, connId := range cons {
			conn := global.Ws_.ConnList[connId]
			if conn.RoomNo != "" && global.Ws_.RoomList[conn.RoomNo] != nil && global.Ws_.RoomList[conn.RoomNo][connId] != "" {
				delete(global.Ws_.RoomList[conn.RoomNo], conn.Id)
				reply.Code = "200"
				reply.Data["msg"] = "您已退出房间"
				reply.Data["uid"] = conn.Uid

				(&service2.ReplyBody{}).SendMsgToConn(reply, conn.Id)
				var messageProto ProtoModel.MessageProto
				messageProto.Action = "user_out_room"
				messageProto.Title = "房间消息"
				messageProto.Content = make(map[string]string)
				messageProto.Content["roomNo"] = conn.RoomNo
				messageProto.Content["uid"] = conn.Uid
				messageProto.Content["nickname"] = conn.Nickname
				messageProto.Content["avatar"] = conn.Avatar
				messageProto.Content["msg"] = conn.Nickname + "退出房间"
				(&service2.Message{}).SendMsgToRoom(&messageProto, conn.RoomNo)
				global.Ws_.ConnList[connId].RoomNo = ""
			}
		}
		b.Ok("成功")
	}
	b.Fail("链接不存在")
}
func (*Bind) BindUser(b *service2.Req) {
	// 接收post参数
	body, err := ioutil.ReadAll(b.R.Body)
	if err != nil {
		log.Println(err)
	}
	//var reg model.LoginModelReg
	userBind := model.UserBindProto{}
	json.Unmarshal(body, &userBind)

	if len(userBind.Uid) < 1 {
		b.Fail("用户uid长度不能少于1位")
		return
	}
	if len(userBind.Cid) < 1 {
		b.Fail("用户Cid长度不能少于1位")
		return
	}
	conn := global.Ws_.ConnList[userBind.Cid]
	if conn == nil {
		b.Fail("用户链接已断开")
		return
	}
	conn.User.AppVersion = userBind.AppVersion
	conn.User.Channel = userBind.Channel
	conn.User.DeviceName = userBind.DeviceName
	conn.User.DeviceId = userBind.DeviceId
	conn.User.OsVersion = userBind.OsVersion
	conn.User.Language = userBind.Language
	conn.Did = userBind.DeviceId
	//conn.User.
	//global.Ws_.ConnList[global.Inc().Id]=conn
	var uid = userBind.Uid
	conn.Uid = uid
	userConn := global.Ws_.UserList[uid]
	if userConn == nil {
		userConn = append(userConn[:], conn.Id)
	} else {
		var has = false
		for _, cid := range userConn {
			if cid == conn.Id {
				has = true
			}
		}
		//未绑定过 需要添加绑定，绑定过就忽略
		if has == false {
			userConn = append(userConn[:], conn.Id)
		}
	}
	global.Ws_.UserList[uid] = userConn
	global.Ws_.ConnList[conn.Id] = conn
	//json.Unmarshal(userBind, &redata)
	reply := &ProtoModel.ReplyBodyProto{
		Key:  "user_bind",
		Code: "200",
		Data: make(map[string]string),
	}
	(&service2.ReplyBody{}).SendMsgToConn(reply, conn.Id)

	b.Ok("绑定成功")

}
